package vn.fpt.fsoft.coreframework;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;

public class Projectile {
	private Point position;
	private int speed;
	private Color color;
	private BufferedImage bufferedImage;
	private boolean isVisible;
	private Player firedByPlayer;
	private int direction;

	/**
	 * @return the firedBy
	 */
	public Player getFiredByPlayer() {
		return firedByPlayer;
	}

	/**
	 * @param firedBy the firedBy to set
	 */
	public void setFiredByPlayer(Player firedByPlayer) {
		this.firedByPlayer = firedByPlayer;
	}

	/**
	 * @return the isVisible
	 */
	public boolean isVisible() {
		return isVisible;
	}

	/**
	 * @param isVisible the isVisible to set
	 */
	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}

	/**
	 * Get value of point.
	 * 
	 * @return the point
	 */
	public Point getPosition() {
		return position;
	}

	/**
	 * Set the value for point.
	 * 
	 * @param point
	 *            the point to set
	 */
	public void setPosition(Point position) {
		this.position = position;
	}

	/**
	 * Get value of speed.
	 * 
	 * @return the speed
	 */
	public int getSpeed() {
		return speed;
	}

	/**
	 * Set the value for speed.
	 * 
	 * @param speed
	 *            the speed to set
	 */
	public void setSpeed(int speed) {
		this.speed = speed;
	}

	/**
	 * Get value of color.
	 * 
	 * @return the color
	 */
	public Color getColor() {
		return color;
	}

	/**
	 * Set the value for color.
	 * 
	 * @param color
	 *            the color to set
	 */
	public void setColor(Color color) {
		this.color = color;
	}

	public Projectile() {

	}
	Sprite sprite;
	public Projectile(Point position, int direction, Player firedByPlayer) {
		sprite = new Sprite();
		this.bufferedImage = sprite.getSprite("images/bullet.png");
//		this.position = updatePositin(position);
		this.speed = 1;
		this.color = Color.black;
		this.isVisible = true;
		this.firedByPlayer = firedByPlayer;
		changeDirection(direction);
		this.direction = direction;
		updatePositin(position);
		
	}
	
	public void updatePositin(Point newPosition) {
		position = newPosition;
		position.x -= 4;
		position.y -= 4;
		switch (direction) {
		case Player.DIRECTION_DOWN:
			position.y += 16;
			break;
		case Player.DIRECTION_UP:
			position.y -= 16;
			break;
		case Player.DIRECTION_LEFT:
			position.x -= 16;
			break;
		case Player.DIRECTION_RIGHT:
			position.x += 16;
			break;
		}
//		return new Point(newPosition.x, newPosition.y);
	}
	public boolean changeDirection(int newDirection) {
		double angel = 0;
		boolean changed = direction != newDirection;
		int part = Math.abs(direction - newDirection);
		if (part == 1) {
			angel = Math.PI / 2;
		} else if (part == 2) {
			angel = Math.PI;
		} else if (part == 3) {
			angel = Math.PI * 1.5;
		} else {
			angel = 0;
		}
		
		if (direction - newDirection < 0) {
			bufferedImage = sprite.rotateImage(bufferedImage, angel);
		} else {
			bufferedImage = sprite.rotateImage(bufferedImage, -angel);
		}
		direction = newDirection;
		return changed;
	}
	
	public void fire() {
		switch (direction) {
		case Player.DIRECTION_DOWN:
			position.y += speed;
			break;
		case Player.DIRECTION_UP:
			position.y -= speed;
			break;
		case Player.DIRECTION_LEFT:
			position.x -= speed;
			break;
		case Player.DIRECTION_RIGHT:
			position.x += speed;
			break;
		}
	}

	public void fire(int speed, char c) {
		this.speed = speed;
//		switch (c) {
//		case ('B'):
//			this.point.y = this.point.y - 1;
//			break;
//		case ('N'):
//			this.point.y = this.point.y + 1;
//			break;
//		case ('T'):
//			this.point.y = this.point.x - 1;
//			break;
//		case ('D'):
//			this.point.y = this.point.x + 1;
//			break;
//		}
	}

	public void draw(Graphics2D g2d) {
		// g2d.setColor(this.color);
		// g2d.fillRoundRect(this.point.x, this.point.y, 5, 5, 5, 5);
		g2d.drawImage(this.bufferedImage, position.x, position.y, null);

	}
}
